package paneles;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.PanelUI;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import listados.ListadoDialog;

import org.hibernate.Session;

import datos.Cliente;
import datos.ProfesionalRealizaServicio;

import accesoADatos.AccesoADatos;

public class PanelHistorial extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTable tblHistorial	=null;
	
	private JPanel pnlFiltros	=null;
	private JPanel pnlTabla		=null;
	
	private JScrollPane scrTabla=null;
	
	private	JComboBox cmbClientes =null;
	
	private static final String CLIENTE	="Historial del Cliente:";
	private static final String FECHA		="Fecha";
	private static final String SERVICIO	="Servicio";
	private static final String COMENTARIO	="Comentario";
	private static final String	TIT_HIST	="HISTORIAL DE LOS CLIENTES";
	private static final String	PROFESIONAL ="Profesional";
	
	public JComboBox getCmbClientes() {
		cmbClientes=new JComboBox();
		List l=AccesoADatos.getInstance().getClientes();
		cmbClientes.setModel(new DefaultComboBoxModel(l.toArray()));
		cmbClientes.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				eventActionPerformed(arg0);
				
			}
		});
		cmbClientes.setBackground(new Color(255,255,255));
		cmbClientes.setForeground(new Color(0,0,0));
		return cmbClientes;
	}

	public PanelHistorial(){
		super();
		initGUI();
	}
	
	private void initGUI(){

		this.setPreferredSize(new java.awt.Dimension(1024, 200));
		//this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0,0,0)),TIT_HIST, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma",1,14), new java.awt.Color(0,0,0)));
		this.setBorder(BorderFactory.createLineBorder(new Color(255,0,0), 10, true));
		this.setLayout(new BorderLayout(0, 0));
		this.setBackground(new Color(240,240,240));
		this.add(getPnlFiltros(),BorderLayout.WEST);
		this.add(getPnlTabla(),BorderLayout.CENTER);
		cargarTabla((Cliente)cmbClientes.getSelectedItem());
	
	}

	public JTable getTblHistorial() {
		tblHistorial = new JTable();
		tblHistorial.setPreferredSize(new Dimension(950, 155));
		tblHistorial.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				FECHA, PROFESIONAL, SERVICIO, COMENTARIO}
		){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column){
				return false;
			}
		});
		tblHistorial.getColumnModel().getColumn(0).setMaxWidth(120);
		tblHistorial.getColumnModel().getColumn(1).setMaxWidth(150);
		tblHistorial.getColumnModel().getColumn(2).setMaxWidth(200);
		tblHistorial.getColumnModel().getColumn(0).setMinWidth(120);
		tblHistorial.getColumnModel().getColumn(1).setMinWidth(150);
		tblHistorial.getColumnModel().getColumn(2).setMinWidth(200);
		//tblHistorial.setIntercellSpacing(new Dimension(5,0));
		tblHistorial.setBackground(Color.red);
		tblHistorial.setGridColor(new Color(255,0,0));
		RendererHistorial rh = new RendererHistorial();
		tblHistorial.setDefaultRenderer(Object.class, rh);
		tblHistorial.setRowHeight(25);
		tblHistorial.setOpaque(true);
		tblHistorial.getTableHeader().setDefaultRenderer(new RendererHeaderHistorial());
		return tblHistorial;
	}

	public JPanel getPnlFiltros() {
		pnlFiltros=new JPanel();
		pnlFiltros.setBackground(Color.RED);
		pnlFiltros.setLayout(new FlowLayout(FlowLayout.CENTER));
		pnlFiltros.setPreferredSize(new Dimension(150,200));
		JLabel lblClientes=new JLabel(CLIENTE);
		lblClientes.setForeground(new Color(255,255,255));
		lblClientes.setFont(new Font("Calibri", Font.BOLD, 16));
		lblClientes.setPreferredSize(new Dimension(150,25));
		pnlFiltros.add(lblClientes);
		pnlFiltros.add(getCmbClientes());
		return pnlFiltros;
	}

	public JPanel getPnlTabla() {
		pnlTabla= new JPanel();
		pnlTabla.setOpaque(true);
		pnlTabla.setBackground(Color.RED);
		pnlTabla.setLayout(new BorderLayout());
		pnlTabla.add(this.getScrTabla(),BorderLayout.CENTER);
		return pnlTabla;
	}

	public JScrollPane getScrTabla() {
		scrTabla= new JScrollPane();
		scrTabla.setOpaque(true);
		scrTabla.setBackground(new Color(255,0,0));
		scrTabla.setViewportView(getTblHistorial());
		scrTabla.setBorder(null);
		return scrTabla;
	}
	private Vector cargarVector(ProfesionalRealizaServicio pRS){
		Vector v= new Vector();
		Session session=AccesoADatos.getInstance().getSession();
		session.beginTransaction();
		v.add(ListadoDialog.FormatearFecha(pRS.getFecha()));
		v.add(pRS.getProfesional().toString());
		v.add(pRS.getServicio().toString());
		v.add(pRS.getComentario());
		session.close();
		
		return v;
	}
	
	protected void cargarTabla(Cliente c) {
		/*tblHistorial.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					FECHA, PROFESIONAL, SERVICIO, COMENTARIO}
			));*/
		List l = AccesoADatos.getInstance().getHistorialCliente(c);
		((DefaultTableModel) (this.tblHistorial.getModel())).setRowCount(0);
		for (int i=0;i<l.size(); i++){
			Vector v=cargarVector((ProfesionalRealizaServicio)l.get(i));
			((DefaultTableModel) (this.tblHistorial.getModel())).addRow(v);
		}
		int tabla = (tblHistorial.getRowCount()-1)*31;
		if(tabla > 156)
			tblHistorial.setPreferredSize(new Dimension(950,tabla));
		else
			tblHistorial.setPreferredSize(new Dimension(950,156));
		AccesoADatos.getInstance().getSession().close();
	}
	
	public void eventActionPerformed(ActionEvent e){
		cargarTabla((Cliente)cmbClientes.getSelectedItem());
	}
	
}
